﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace todolist
{
    public partial class StatisticsForm : Form
    {
        public StatisticsForm()
        {
            InitializeComponent();
        }

        TasksEntities db = new TasksEntities();

        // ===============================
        //  Формирование статистики
        // ===============================
        private void GenStat()
        {
            DateTime beginPeriod = periodBeginDateTimePicker.Value;
            DateTime endPeriod = periodEndDateTimePicker.Value;

            int current_count = 0;
            int current_overdue_count = 0;
            int done_count = 0;
            int done_in_time_count = 0;
            int done_overdue_count = 0;

            foreach (var item in db.Tasks)
            {
                //current
                if (item.added_date <= endPeriod && (item.actual_date > endPeriod || item.actual_date == null))
                {
                    current_count++;
                    // Overdue by the end of the period
                    if (item.due_date < endPeriod)
                    {
                        current_overdue_count++;
                    }
                }

                // done during the period
                if (item.actual_date >= beginPeriod && item.actual_date <= endPeriod)
                {
                    done_count++;
                    if (item.due_date >= item.actual_date)
                    {
                        done_in_time_count++;
                    }
                    else if (item.due_date <= item.actual_date)
                    {
                        done_overdue_count++;
                    }

                }
            }

            currentTasksLabel.Text = "Количество текущих задач - " + current_count.ToString();
            overdueCurrentTasksLabel.Text = "Из них просрочено на конец периода - " + current_overdue_count.ToString();
            doneTasksLabel.Text = "Количество выполненных задач - " + done_count.ToString();
            doneInTimeLabel.Text = "Из них выполнено вовремя - " + done_in_time_count.ToString();
            overdueDoneTasksLabel.Text = "Выполнено позже срока - " + done_overdue_count.ToString();

        }

        // ===============================
        //  ЗАГРУЗКА ФОРМЫ
        // ===============================
        private void Form2_Load(object sender, EventArgs e)
        {
            GenStat();
        }

        // ===============================
        //  Закрытие формы
        // ===============================
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        // ===============================
        //  Задание нового периода
        // ===============================
        private void periodApplyButton_Click(object sender, EventArgs e)
        {
            GenStat();
        }
    }
}